<?php

namespace app\api\controller;

use app\api\model\RecitingMission;
use app\api\model\RecitingQuestionAnswer;
use app\api\model\Homework;
use app\common\controller\Api;
use fast\Random;
use think\Request;

class Wrong extends Api
{


    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\api\model\RecitingMission();
    }

    public function index()
    {
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        $userid = $this->auth->id;
        $list = $this->model->where(['user_id'=>$userid,'mission_type'=>['in','2,3']])->limit(($page - 1)*$pageSize,$pageSize)->select();
        $homework = new Homework();
        $ans = new RecitingQuestionAnswer();
        foreach ($list as &$va){
            $operation = $homework->where('id',$va['mission_id'])->find();
            $va['operation'] = $operation;
            $va['endtime'] = $operation['deadlinetime'];
            $va['isend'] = 0;
            if (strtotime($operation['deadlinetime'])<=time()) {
                $va['isend'] = 1;
            }
            $va['cuonum'] = $ans->where(['mission_id'=>$va['mission_id'],'source_id'=>$va['source_id']])->where('answer_options_id','!=','true_answer_options_id')->count();
        }
        unset($va);
        $this->success('请求成功',$list);
    }

    public function detail()
    {
        $userid = $this->auth->id;
        $id = input('id/d');
        $detail = $this->model->where(['user_id'=>$userid,'id'=>$id])->find();
        $operation = $homework->where('id',$detail['mission_id'])->find();
        $detail['operation'] = $operation;
        $detail['endtime'] = $operation['deadlinetime'];
        $detail['isend'] = 0;
        if (strtotime($operation['deadlinetime'])<=time()) {
            $detail['isend'] = 1;
        }
        $detail['cuonum'] = $ans->where(['mission_id'=>$detail['mission_id'],'source_id'=>$detail['source_id']])->where('answer_options_id','!=','true_answer_options_id')->count();
        $this->success('请求成功',$detail);
    }
    
}